package com.yixuan.mt.console.controller;

import com.yixuan.mt.common.pojo.EventsListInfo;
import com.yixuan.mt.common.request.EventsMysqlRequest;
import com.yixuan.mt.common.request.EventsRedisRequest;
import com.yixuan.mt.common.response.Result;
import com.yixuan.mt.common.service.EventsService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/op/events")
public class EventsController {

    @Autowired
    private EventsService eventsService;

    /**
     * 添加mysql事件目的地
     */
    @PostMapping("/mysql")
    public Result<?> postMysqlEvents(@Valid @RequestBody EventsMysqlRequest eventsMysqlRequest) throws SQLException {
        eventsService.postMysqlEvents(eventsMysqlRequest);
        return Result.success();
    }

    /**
     * 添加redis事件目的地
     */
    @PostMapping("/redis")
    public Result<?> postRedisEvents(@Valid @RequestBody EventsRedisRequest eventsRedisRequest) {
        eventsService.postRedisEvents(eventsRedisRequest);
        return Result.success();
    }

    /**
     * 删除事件目的地
     */
    @DeleteMapping("/{type}/{name}")
    public Result<?> deleteEvents(@PathVariable String type, @PathVariable String name) {
        eventsService.deleteEvents(type, name);
        return Result.success();
    }

    /**
     * 获取事件目的地列表
     */
    @GetMapping
    public Result<List<EventsListInfo>> getEventsList() throws SQLException {
        return Result.success(eventsService.getEventsList());
    }
}
